package com.cby.android.library.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import com.cby.android.library.R

abstract class BaseFragment : Fragment() {

    /** 根布局 */
    var rootView: View? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        parseArgs()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        rootView = LayoutInflater.from(activity).inflate(getLayoutId(), container, false)
        initView(rootView!!)
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        onViewInit(view)
        onDataInit()
    }

    open fun parseArgs() {

    }

    abstract fun getLayoutId(): Int

    abstract fun onViewInit(view: View)

    abstract fun onDataInit()

    open fun initView(view: View){

    }

    fun <V : View?> findViewById(@IdRes id: Int): V? {
        return rootView?.findViewById(id)
    }
}

class BlankFragment : BaseFragment() {

    override fun getLayoutId() = R.layout.fragment_blank

    override fun onViewInit(view: View) {
    }

    override fun onDataInit() {
    }

}